package com.example.frank.login.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.frank.login.R;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity {

    private AutoCompleteTextView username;
    private EditText passwd;
    private TextView massage_login,forgot_passwd;
    private Button login_button,register_button;
    private String str_username,str_passwd;
    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = findViewById(R.id.username);
        passwd = findViewById(R.id.passwd);
        massage_login = findViewById(R.id.massage_login);
        forgot_passwd = findViewById(R.id.forgot);
        login_button = findViewById(R.id.login_button);
        register_button = findViewById(R.id.register_button);

        login_button_onclick();
        register_button_onclick();

}

//    前，后台用户名密码传递
    public void loginByHttpClientPost(String userName, String Passwd) throws IOException {
        String path = "http://10.0.0.15:3000/user/logincheck";
        HttpPost httpPost = new HttpPost(path);
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("username", userName));
        list.add(new BasicNameValuePair("password", Passwd));

        httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
        String results = "";

        try {
            HttpResponse response = new DefaultHttpClient().execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                results = EntityUtils.toString(entity, HTTP.UTF_8);
                switch (results) {
                    case "1":
                        Intent intent = new Intent(LoginActivity.this, MainActivity1.class);
                        intent.putExtra("idcard", userName);
                        startActivity(intent);
                        Looper.prepare();
                        Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
                        break;
                    case "0":
                        Looper.prepare();
                        Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                }
            } else {
                Looper.prepare();
                Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        } catch (Exception e) {
            results = e.toString();
            Looper.prepare();
            Toast.makeText(LoginActivity.this, results, Toast.LENGTH_SHORT).show();
            Looper.loop();

        }

    }

//    请求登录
    public void login_button_onclick(){

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                str_username = username.getText().toString();
                str_passwd = passwd.getText().toString();
//                SharedPreferences.Editor editor =sp.edit();
                try{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                loginByHttpClientPost(str_username,str_passwd);
                            } catch (IOException e) {
                                String err = e.toString();
                                Toast.makeText(LoginActivity.this,err,Toast.LENGTH_SHORT).show();
                            }
                        }
                    }).start();
                } catch (Exception e){
                    Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

//    跳转注册
    public void register_button_onclick(){
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,MainActivity1.class);
                startActivity(intent);
            }
        });
    };

}
